﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Slippor.Common.PluginFramework;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.BaseForm.Properties;
using System.Drawing;

namespace Slippor.Douban.Radio.BaseForm
{
    public partial class ConfigForm : Form
    {
        private DoubanForm _parentDoubanForm;
        private PluginManager _pluginManager;

        public ConfigForm()
        {
            InitializeComponent();
            InitializeText();
        }

        private void InitializeText()
        {
            this.Text = StringResource.D_ConfigForm;
            label1.Text = StringResource.D_label1;
            label2.Text = StringResource.D_label2;
            label3.Text = StringResource.D_label3;
            label4.Text = StringResource.D_label4;
            label5.Text = StringResource.D_label5;
            label6.Text = StringResource.D_label6;
            label7.Text = StringResource.D_label7;
            label8.Text = StringResource.D_label8;
            label9.Text = StringResource.D_label9;
            label10.Text = StringResource.D_label10;
            label11.Text = StringResource.D_label11;
            label12.Text = StringResource.D_label12;
            labelAppTitle.Text = StringResource.D_labelAppTitle;
            labelAuthor.Text = StringResource.D_labelAuthor;
            labelHead.Text = StringResource.D_labelHead;
            labelOpacity.Text = StringResource.D_labelOpacity;
            labelVersion.Text = StringResource.Version_CurrentVersion;
            buttonCancel.Text = StringResource.D_buttonCancel;
            buttonConfig.Text = StringResource.D_buttonConfig;
            buttonConfirm.Text = StringResource.D_buttonConfirm;
            buttonDelete.Text = StringResource.D_buttonDelete;
            buttonFilterAdd.Text = StringResource.D_buttonFilterAdd;
            buttonPluginUpdate.Text = StringResource.D_buttonPluginUpdate;
            buttonReset.Text = StringResource.D_buttonReset;
            buttonUpdate.Text = StringResource.Version_UpdateCheck;
            checkBoxAutoMin.Text = StringResource.D_checkBoxAutoMin;
            checkBoxAutoRun.Text = StringResource.D_checkBoxAutoRun;
            checkBoxAutoUpdate.Text = StringResource.D_checkBoxAutoUpdate;
            checkBoxDifShortKey.Text = StringResource.D_checkBoxDifShortKey;
            checkBoxFilterChinese.Text = StringResource.D_checkBoxFilterChinese;
            checkBoxFilterEnglish.Text = StringResource.D_checkBoxFilterEnglish;
            checkBoxHotKey.Text = StringResource.D_checkBoxHotKey;
            checkBoxInfo.Text = StringResource.D_checkBoxInfo;
            checkBoxProxy.Text = StringResource.D_checkBoxProxy;
            checkBoxRecord.Text = StringResource.D_checkBoxRecord;
            checkBoxCloseConfirm.Text = StringResource.D_checkBoxCloseConfirm;
            checkBoxShowInTaskBar.Text = StringResource.D_checkBoxShowInTaskBar;
            tabPageAbout.Text = StringResource.D_tabPageAbout;
            tabPageBasic.Text = StringResource.D_tabPageBasic;
            tabPageFilter.Text = StringResource.D_tabPageFilter;
            tabPageHotKey.Text = StringResource.D_tabPageHotKey;
            tabPagePrivate.Text = StringResource.D_tabPagePrivate;
            linkLabelBug.Text = StringResource.D_linkLabelBug;
            linkLabelIcon.Text = StringResource.D_linkLabelIcon;
            linkLabelInfo.Text = StringResource.D_linkLabelInfo;
            linkLabelPlugin.Text = StringResource.D_linkLabelPlugin;
        }

        public DoubanForm ParentDoubanForm
        {
            get { return _parentDoubanForm; }
            set { _parentDoubanForm = value; }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            InitializeConfigs();
            InitializePlugins();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            ResetConfigs();
            RadioUtility.ReleaseMemory();
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            ConfirmConfigs();
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(Resources.ConfigForm_Reset_Confirm, StringResource.Message_Alert, MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                StaticResources.Settings.Reset();
                StaticResources.Settings.Save();
                _parentDoubanForm.OnResetting();
                InitializeConfigs();
            }
        }

        private void InitializeConfigs()
        {
            switch(_parentDoubanForm.Language)
            {
                case "Auto":
                    comboBoxLanguage.SelectedIndex = 0;
                    break;
                case "zh-TW":
                    comboBoxLanguage.SelectedIndex = 2;
                    break;
                case "zh-CN":
                    comboBoxLanguage.SelectedIndex = 1;
                    break;
            }

            labelVersion.Text = StringResource.Version_CurrentVersion + _parentDoubanForm.CurrentVersion.VersionCode;
            if (_parentDoubanForm.CurrentVersion.IsLatest)
            {
                labelVersion.Text = labelVersion.Text + String.Format("({0})",StringResource.Version_Latest);
            }
            if (VersionControl.IsChecking)
            {
                buttonUpdate.Text = StringResource.Version_UpdateChecking;
                buttonUpdate.Enabled = false;
            }
            else
            {
                buttonUpdate.Text = StringResource.Version_UpdateCheck;
                buttonUpdate.Enabled = true;
            }
            checkBoxAutoRun.Checked = _parentDoubanForm.AutoRun;
            checkBoxAutoUpdate.Checked = _parentDoubanForm.AutoUpdate;
            checkBoxHotKey.Checked = _parentDoubanForm.EnableHotKey;
            checkBoxProxy.Checked = _parentDoubanForm.UseProxy;
            textBoxAdd.Text = _parentDoubanForm.ProxyAddress;
            textBoxPort.Text = _parentDoubanForm.ProxyPort;
            //checkBoxButtonShow.Checked = _parentDoubanForm.IsButtonsShow;
            checkBoxShowInTaskBar.Checked = _parentDoubanForm.IsTaskbarShow;
            if (_parentDoubanForm.EnableHotKey)
            {
                _parentDoubanForm.UnHotkeys();
            }
            checkBoxRecord.Checked = _parentDoubanForm.RecordHistory;
            checkBoxAutoMin.Checked = _parentDoubanForm.AutoMin;
            checkBoxAutoUpdate.Checked = _parentDoubanForm.AutoUpdate;
            checkBoxInfo.Checked = _parentDoubanForm.GetInfo;
            checkBoxCloseConfirm.Checked = _parentDoubanForm.CloseConfirm;
            checkBoxFilterEnglish.Checked = _parentDoubanForm.Filters.ContainsKey(EnglishFilter.Key);
            checkBoxFilterChinese.Checked = _parentDoubanForm.Filters.ContainsKey(ChineseFilter.Key);
            SetCustomFilters(_parentDoubanForm.Filters);
            SetHotKeys();

            _originalOpacity = _parentDoubanForm.FormOpacity;
            trackBarOpacity.Value = (int) (_originalOpacity*100);
        }

        private void ConfirmConfigs()
        {
            switch (comboBoxLanguage.SelectedIndex)
            {
                case 0:
                    _parentDoubanForm.Language = "Auto";
                    break;
                case 1:
                    _parentDoubanForm.Language = "zh-CN";
                    break;
                case 2:
                    _parentDoubanForm.Language = "zh-TW";
                    break;
            }

            _parentDoubanForm.AutoRun = checkBoxAutoRun.Checked;
            _parentDoubanForm.AutoUpdate = checkBoxAutoUpdate.Checked;
            _parentDoubanForm.EnableHotKey = checkBoxHotKey.Checked;
            _parentDoubanForm.RecordHistory = checkBoxRecord.Checked;
            _parentDoubanForm.AutoMin = checkBoxAutoMin.Checked;
            _parentDoubanForm.AutoUpdate = checkBoxAutoUpdate.Checked;
            _parentDoubanForm.GetInfo = checkBoxInfo.Checked;
            _parentDoubanForm.CloseConfirm = checkBoxCloseConfirm.Checked;
            //_parentDoubanForm.IsButtonsShow = checkBoxButtonShow.Checked;
            _parentDoubanForm.IsTaskbarShow = checkBoxShowInTaskBar.Checked;
            _parentDoubanForm.ProxyAddress = textBoxAdd.Text;
            _parentDoubanForm.ProxyPort = textBoxPort.Text;
            _parentDoubanForm.UseProxy = checkBoxProxy.Checked;
            lock (_parentDoubanForm.Filters.ForLock)
            {
                _parentDoubanForm.Filters.NormalClear();
                if (checkBoxFilterEnglish.Checked)
                {
                    _parentDoubanForm.Filters[EnglishFilter.Key] = new EnglishFilter();
                }
                if (checkBoxFilterChinese.Checked)
                {
                    _parentDoubanForm.Filters[ChineseFilter.Key] = new ChineseFilter();
                }
                foreach (String filter in listBoxFilter.Items)
                {
                    if (!String.IsNullOrEmpty(filter))
                    {
                        _parentDoubanForm.Filters[filter] = new CustomFilter(filter);
                    }
                }
                _parentDoubanForm.Filters.Update();
                _parentDoubanForm.Filter();
            }
            foreach (Command command in _commands.Keys)
            {
                ICommand commander = CommandFactory.GetCommand(command, RadioType.PrivateRadio);
                StaticResources.SettingManager.SetString(commander.ID, _commands[command].Text);
            }
            _originalOpacity = (trackBarOpacity.Value/(double) 100);
            ParentDoubanForm.FormOpacity = _originalOpacity;
        }

        private void ResetConfigs()
        {
            if (_parentDoubanForm.EnableHotKey)
            {
                _parentDoubanForm.RegHotkeys();
            }
            _parentDoubanForm.Opacity = _originalOpacity;
        }

        #region Plugins
        private void InitializePlugins()
        {
            using (var pluginWorker = new BackgroundWorker())
            {
                pluginWorker.DoWork += (sender, e) =>
                    {
                         _pluginManager = new PluginManager(_parentDoubanForm)
                                                     {
                                                         PluginPath = StaticResources.PluginPath
                                                     };
                        _pluginManager.InitialPlugins();
                    };
                pluginWorker.RunWorkerCompleted += (sender,e)=>
                    {
                        SetListPlugins();
                    };
                pluginWorker.RunWorkerAsync();
            }
        }

        private void SetListPlugins()
        {
            foreach (PluginInfo pInfo in _pluginManager.PluginInfos)
            {
                var lvi = new ListViewItem(pInfo.PluginName);
                if (!String.IsNullOrEmpty(pInfo.DisplayName))
                {
                    lvi.Text = pInfo.DisplayName;
                }
                if (pInfo.PluginVersion != null)
                {
                    lvi.SubItems.Add(pInfo.PluginVersion.Version.ToString());
                }
                else
                {
                    lvi.SubItems.Add("-");
                }
                lvi.SubItems.Add(pInfo.PluginFile);
                switch (pInfo.Category)
                {
                    case PluginCategory.Search:
                        listViewPlugins.Groups[0].Items.Add(lvi);
                        break;
                    case PluginCategory.Filter:
                        listViewPlugins.Groups[1].Items.Add(lvi);
                        break;
                    case PluginCategory.Handler:
                        listViewPlugins.Groups[2].Items.Add(lvi);
                        break;
                    case PluginCategory.Tracker:
                        listViewPlugins.Groups[3].Items.Add(lvi);
                        break;
                    default:
                        listViewPlugins.Groups[4].Items.Add(lvi);
                        break;
                }
                listViewPlugins.Items.Add(lvi);
                lvi.Checked = pInfo.IsEnabled;
                lvi.Tag = pInfo;
            }
        }

        //private void CofirmPlugins()
        //{
        //    foreach (ListViewItem item in listViewPlugins.Items)
        //    {
        //        var pInfo = item.Tag as PluginInfo;
        //        if (pInfo != null) pInfo.IsEnabled = item.Checked;
        //    }
        //    _pluginManager.UpdatePlugins();
        //}

        private void listViewPlugins_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewPlugins.SelectedItems.Count == 1)
            {
                var pInfo = listViewPlugins.SelectedItems[0].Tag as PluginInfo;
                SetPluginDes(pInfo);
            }
        }

        private void listViewPlugins_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            var pInfo = listViewPlugins.Items[e.Index].Tag as PluginInfo;
            if (pInfo != null && pInfo.IsEnabled != (e.NewValue == CheckState.Checked))
            {
                pInfo.IsEnabled = e.NewValue == CheckState.Checked;
                SetPluginDes(pInfo);
                _pluginManager.UpdatePlugin(pInfo);
            }
        }

        private void listViewPlugins_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
        }

        private static readonly Dictionary<String, KeyValuePair<bool, String>> _pluginUpdates = new Dictionary<String, KeyValuePair<bool, String>>();
        private void SetPluginDes(PluginInfo pInfo)
        {
            if (pInfo != null)
            {
                String text = String.Empty;
                if (pInfo.Author != null)
                {
                    text = String.Format("{2} : {0} ({1})", pInfo.Author.AuthorName, pInfo.Author.AuthorUrl, StringResource.Plugin_Author) + Environment.NewLine;
                }
                text = text + pInfo.PluginDescription;
                textBoxDescription.Text = text;
                buttonConfig.Visible = pInfo.IsConfigable;
                buttonConfig.Enabled = pInfo.IsEnabled;
                buttonPluginUpdate.Visible = pInfo.IsVersionable;
                buttonConfig.Tag = pInfo;
                buttonPluginUpdate.Tag = pInfo;
                SetUpdateButton(pInfo);
            }
        }

        private void buttonConfig_Click(object sender, EventArgs e)
        {
            var pInfo = ((Control)sender).Tag as PluginInfo;
            if (pInfo != null)
            {
                var plugin = _pluginManager.GetPluginInstance(pInfo);
                if (plugin != null)
                {
                    if (pInfo.Config(ref plugin))
                    {
                        _pluginManager.ReloadPlugin(pInfo);
                    }
                }
            }
        }

        private readonly List<IVersion> _handledVersions = new List<IVersion>();
        private void buttonPluginUpdate_Click(object sender, EventArgs e)
        {
            var pInfo = ((Control)sender).Tag as PluginInfo;
            if (pInfo != null && pInfo.IsVersionable)
            {
                if(buttonPluginUpdate.Text != StringResource.Plugin_Update)
                {
                    IVersion pVersion = pInfo.PluginVersion;
                    _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(false, StringResource.Plugin_Checking);
                    if (!_handledVersions.Contains(pVersion))
                    {
                        pVersion.GetLatestVersionCompleted += (lsender, le) =>
                        {
                            PluginVersion version = le.Version;
                            if (le.Succeed && version != null)
                            {
                                if (version.Version.CompareTo(pVersion.Version) <= 0)
                                {
                                    _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(true, StringResource.Plugin_Latest);
                                }
                                else
                                {
                                    if (MessageBox.Show(String.Format(StringResource.Plugin_UpdateMessage, pInfo.DisplayName, version.Description), String.Empty, MessageBoxButtons.OKCancel) == DialogResult.OK)
                                    {
                                        Update(pInfo);
                                    }
                                    else
                                    {
                                        _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(true, StringResource.Plugin_Update);
                                    }
                                }
                            }
                            else
                            {
                                _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(true, StringResource.Plugin_CheckFailed);
                            }
                            if (buttonPluginUpdate.Tag == pInfo)
                            {
                                SetUpdateButton(pInfo);
                            }
                        };
                        _handledVersions.Add(pVersion);
                    }
                    pVersion.GetLatestVersionAsync();
                    SetUpdateButton(pInfo);
                }
                else
                {
                    Update(pInfo);
                }
            }
        }

        private void Update(PluginInfo pInfo)
        {
            IVersion pVersion = pInfo.PluginVersion;
            _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(false, StringResource.Plugin_Updating);
            pVersion.UpdateCompleted += (uSender, uE) =>
            {
                if (!uE.Succeed)
                {
                    _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(true, StringResource.Plugin_UpdateFailed);
                }
                else
                {
                    _pluginUpdates[pInfo.PluginName] = new KeyValuePair<bool, string>(true, StringResource.Plugin_UpdateSucceed);
                }
                if (buttonPluginUpdate.Tag == pInfo)
                {
                    SetUpdateButton(pInfo);
                }
            };
            pVersion.UpdateAsync();
        }

        private void SetUpdateButton(PluginInfo pInfo)
        {
            if (_pluginUpdates.ContainsKey(pInfo.PluginName))
            {
                buttonPluginUpdate.Enabled = _pluginUpdates[pInfo.PluginName].Key;
                buttonPluginUpdate.Text = _pluginUpdates[pInfo.PluginName].Value;
            }
            else
            {
                buttonPluginUpdate.Enabled = true;
                buttonPluginUpdate.Text = StringResource.Version_UpdateCheck;
            }
        }

        private void textBoxDescription_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            RadioUtility.Browse(e.LinkText);
        }
        #endregion

        #region HotKeys

        private Dictionary<Command, TextBox> _commands;

        private void SetHotKeys()
        {
            _commands = new Dictionary<Command, TextBox>
                            {
                                {Command.Control, textBoxControl},
                                {Command.Fav, textBoxFav},
                                {Command.UnFav, textBoxUnFav},
                                {Command.Next, textBoxNext},
                                {Command.Del, textBoxDel},
                                {Command.Search, textBoxSearch},
                                {Command.Notify, textBoxDisplay},
                                {Command.ShowInfo, textBoxShow},
                            };
            foreach (Command command in _commands.Keys)
            {
                ICommand commander = CommandFactory.GetCommand(command, RadioType.PrivateRadio);
                if (String.IsNullOrEmpty(StaticResources.SettingManager.GetString(commander.ID)))
                {
                    _commands[command].Text = String.Empty;
                }
                else
                {
                    _commands[command].Text = StaticResources.SettingManager.GetString(commander.ID);
                }
            }
            ICommand unfavCommander = CommandFactory.GetCommand(Command.UnFav, RadioType.PrivateRadio);
            if (!String.IsNullOrEmpty(StaticResources.SettingManager.GetString(unfavCommander.ID)))
            {
                checkBoxDifShortKey.Checked = true;
            }
        }

        private void textBoxHotkey_KeyDown(object sender, KeyEventArgs e)
        {
            var box = sender as TextBox;
            String text = String.Empty;
            if (e.Control)
            {
                text = text + '+' + "Ctrl";
            }
            if (e.Alt)
            {
                text = text + '+' + "Alt";
            }
            if (e.Shift)
            {
                text = text + '+' + "Shift";
            }
            if (e.Modifiers == Keys.LWin || e.Modifiers == Keys.RWin)
            {
                text = text + '+' + "Win";
            }
            if (e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z || e.KeyCode > Keys.F1 && e.KeyCode <= Keys.F12 || e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 || e.KeyCode >= Keys.BrowserBack && e.KeyCode <= Keys.MediaPlayPause || e.KeyCode >= Keys.Multiply && e.KeyCode <= Keys.Divide)
            {
                text = text + '+' + e.KeyCode;
            }
            else if (e.KeyCode >= Keys.PageUp && e.KeyCode <= Keys.Help)
            {
                text = text + '+' + e.KeyCode;
            }
            text = text.TrimStart('+');
            if (box != null) box.Text = text;
            e.Handled = true;
        }

        private void textBoxHotkey_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void textBoxHotkey_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
        }

        private void textBoxHotKey_TextChanged(object sender, EventArgs e)
        {
            var box = sender as TextBox;
            if (box != null)
                if (!String.IsNullOrEmpty(box.Text))
                {
                    if (box.Text[0] >= 'a' && box.Text[0] <= 'z' || box.Text[0] >= '0' && box.Text[0] <= '9')
                    {
                        box.Text = box.Text.Remove(0, 1);
                    }
                }
        }

        private void checkBoxDifShortKey_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxDifShortKey.Checked)
            {
                textBoxUnFav.Enabled = true;
                label2.Text = StringResource.Command_Like;
            }
            else
            {
                textBoxUnFav.Enabled = false;
                textBoxUnFav.Text = String.Empty;
                label2.Text = StringResource.D_label2;
            }
        }
        #endregion

        #region 检查更新

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            buttonUpdate.Text = StringResource.Version_UpdateChecking;
            buttonUpdate.Enabled = false;
            VersionControl.CheckVersion(_parentDoubanForm.CurrentVersion, VersionCheckEnd);
        }

        private void VersionCheckEnd(RadioVersion newVersion)
        {
            if (newVersion.IsNewer(_parentDoubanForm.CurrentVersion))
            {
                DialogResult result = MessageBox.Show(String.Format(StringResource.Message_UpdateAlert, Environment.NewLine + newVersion.VersionDes), StringResource.Message_NewVersion, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    RadioUtility.Browse(String.Format("{0}?s=download&v={1}&t={2}",newVersion.DownloadUrl,_parentDoubanForm.CurrentVersion.VersionCode,_parentDoubanForm.CurrentVersion.VersionMark));
                }
            }
            else
            {
                MessageBox.Show(StringResource.Message_VersionLatest);
                _parentDoubanForm.CurrentVersion.IsLatest = true;
                labelVersion.Text = StringResource.Version_CurrentVersion + _parentDoubanForm.CurrentVersion.VersionCode + String.Format("({0})", StringResource.Version_Latest);
            }
            buttonUpdate.Text = StringResource.Version_UpdateCheck;
            buttonUpdate.Enabled = true;
        }

        #endregion

        #region 过滤
        private void SetCustomFilters(FilterCollection filters)
        {
            listBoxFilter.Items.Clear();
            foreach (String filter in filters.Keys)
            {
                if (!String.IsNullOrEmpty(filter) && filter.IndexOf('#') != 0)
                {
                    listBoxFilter.Items.Add(filter);
                }
            }
        }

        private void checkBoxFilterEnglish_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxFilterEnglish.Checked)
            {
                checkBoxFilterChinese.Checked = false;
            }
        }

        private void checkBoxFilterChinese_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxFilterChinese.Checked)
            {
                checkBoxFilterEnglish.Checked = false;
            }
        }

        private void buttonFilterAdd_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textBoxFilter.Text))
            {
                if (listBoxFilter.Items.Contains(textBoxFilter.Text))
                {
                    MessageBox.Show(StringResource.Message_SameFilter);
                }
                else
                {
                    listBoxFilter.Items.Add(textBoxFilter.Text);
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (listBoxFilter.SelectedItem != null)
            {
                listBoxFilter.Items.Remove(listBoxFilter.SelectedItem);
            }
            {
                var temp = new List<String>();
                foreach (String item in listBoxFilter.SelectedItems)
                {
                    temp.Add(item);
                }
                foreach (String item in temp)
                {
                    listBoxFilter.Items.Remove(item);
                }
            }
        }

        private void listBoxFilter_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0)
            {
                e.DrawBackground();
                e.DrawFocusRectangle();
                e.Graphics.DrawString(listBoxFilter.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor),
                                      e.Bounds);
            }
        }
        #endregion

        #region 透明度

        private double _originalOpacity = 1;

        private void trackBarOpacity_ValueChanged(object sender, EventArgs e)
        {
            double opacity = (trackBarOpacity.Value/(double) 100);
            labelOpacity.Text = opacity.ToString("P");
            _parentDoubanForm.Opacity = opacity;
        }

        #endregion

        #region 点击链接
        private void linkLabelBug_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse(BuildTrackLink("http://slippor.blogbus.com/logs/64747931.html", "bug"));
        }

        private void linkLabelPlugin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse(BuildTrackLink("http://slippor.blogbus.com/logs/62940673.html","plugin"));
        }

        private void linkLabelInfo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse(BuildTrackLink("http://slippor.blogbus.com/logs/62644175.html","info"));
        }

        private void labelAuthor_Click(object sender, EventArgs e)
        {
            RadioUtility.Browse(BuildTrackLink("http://slippor.blogbus.com/", "title"));
        }

        private void linkLabelIcon_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RadioUtility.Browse("http://l-epicerie.blogbus.com/");
        }

        private String BuildTrackLink(String url, String source)
        {
            return String.Format("{0}?s={1}&v={2}&t={3}",url, source,
                                 _parentDoubanForm.CurrentVersion.VersionCode,
                                 _parentDoubanForm.CurrentVersion.VersionMark);
        }
        #endregion

        #region Proxy
        private void checkBoxProxy_CheckStateChanged(object sender, EventArgs e)
        {
            textBoxAdd.Enabled = textBoxPort.Enabled = checkBoxProxy.Checked;
        }
        #endregion
    }
}